Write Web Apps with Dart: Develop and Design by Jack Murphy
Author:Jack Murphy [Murphy, Jack]
Language: eng
Format: epub
Publisher: Pearson Education
Published: 2015-12-08T23:00:00+00:00
Embedding Documents
One of the powerful features of MongoDB is the ability to embed documents. Collections are a great way to create logical groupings, but those separations mean you can’t execute a single query to get data from both collections; you have to issue two separate queries. For a lot of applications, that is perfectly acceptable. However, in cases where performance is a top priority, Mongo offers the capacity to embed documents inside documents.
Embedding allows you to nest objects inside objects. This is a familiar practice to anyone who’s worked with JSON data structures.
Let’s update the collection to use some embedded documents to track game states for each player. First, let’s drop the collection so you can clean up the data. Dropping a collection removes all the documents from a collection.
1. Run the drop() and find() functions:
> db.BaseballStats.drop()
true
> db.BaseballStats.find()
>
Let’s take a look at the JSON structure of the documents you are going to insert.
[
{
"name": "Doe",
"games": [
{
"date": "06-22-2015",
"at_bats": 3,
"hits": 1
},
{
"date": "06-23-2015",
"at_bats": 3,
"hits": 1
},
{
"date": "06-23-2015",
"at_bats": 3,
"hits": 1
}
]
},
{
"name": "Yates",
"games": [
{
"date": "06-22-2015",
"at_bats": 4,
"hits": 2
},
{
"date": "06-23-2015",
"at_bats": 2,
"hits": 0
},
{
"date": "06-23-2015",
"at_bats": 4,
"hits": 4
}
]
}
]
Next, you will insert the above JSON code into your collection. Luckily, the Mongo client supports multiline entries.
2. Construct the opening line of the query, and press Return/Enter.
> db.BaseballStats.insert(
...
You can manually begin entering the JSON code, or you can paste it directly into the command prompt.
3. Enter each new line as needed by pressing the Return/Enter key.
MongoDB understands that you are creating a document. It will wait for the JSON structure to be closed before executing.
4. When you’re finished placing the JSON structure into the MongoDB client, append an additional closing parenthesis, and press Return/Enter.
... )
If you entered the JSON code correctly, Mongo created two new documents for you. Each document contains a field named games that will be an array of nested documents.
5. Run a find() command to take a look at how Mongo has stored your data:
Click here to view code image
> db.BaseballStats.find()
{ "_id" : ObjectId("55953c05a9e58e4c4304eadb"), "name" : "Doe", "games" : [ { "date" : "06-22-2015", "at_bats" : 3, "hits" : 1 }, { "date" : "06-23-2015", "at_bats" : 3, "hits" : 1 }, { "date" : "06-23-2015", "at_bats" : 3, "hits" : 1 } ] }
{ "_id" : ObjectId("55953c05a9e58e4c4304eadc"), "name" : "Yates", "games" : [ { "date" : "06-22-2015", "at_bats" : 4, "hits" : 2 }, { "date" : "06-23-2015", "at_bats" : 2, "hits" : 0 }, { "date" : "06-23-2015", "at_bats" : 4, "hits" : 4 } ] }
6. Query a top-level object using the established query convention:
Click here to view code image
> db.BaseballStats.find({name: "Doe"})
{ "_id" : ObjectId("55953c05a9e58e4c4304eadb"), "name" : "Doe", "games" : [ { "date" : "06-22-2015", "at_bats" : 3, "hits" : 1 }, { "date" : "06-23-2015", "at_bats" : 3, "hits" : 1 }, { "date" : "06-23-2015", "at_bats" : 3, "hits" : 1 } ] }
7. Ask the question “which players had a game with four hits?” by constructing the following query. It will return the entire root object.
Click here to view code image
> db.BaseballStats.find({"games.hits": 4})
{ "_id" : ObjectId("55953c05a9e58e4c4304eadc"),
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9931)
The Mikado Method by Ola Ellnestam Daniel Brolund(9799)
Dependency Injection in .NET by Mark Seemann(9355)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7799)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7573)
Svelte with Test-Driven Development by Daniel Irvine(7341)
Test-Driven Development with PHP 8 by Rainier Sarabia(7080)
Layered Design for Ruby on Rails Applications by Dementyev Vladimir;(6930)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6544)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6432)
Web Development with Django by Ben Shaw Saurabh Badhwar(6395)
React Application Architecture for Production by Alan Alickovic(6121)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5822)
Kotlin in Action by Dmitry Jemerov(5080)
Audition by Ryu Murakami(4600)
Software Architecture for Web Developers by Mihaela Roxana Ghidersa(4544)
Accelerating Server-Side Development with Fastify by Manuel Spigolon Maksim Sinik & Matteo Collina(4397)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4329)
Solidity Programming Essentials by Ritesh Modi(4101)
